﻿package net.htjs.mobile.android.crm;
import net.htjs.mobile.android.crm.util.L;
import android.content.Context;
import android.text.method.Touch;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView;
import android.widget.Toast;
public class MyWebView extends WebView implements OnTouchListener,OnGestureListener{

	private GestureDetector mGestureDetector=null;
	private boolean myTag=false;
	private int screenWidth=240;
	private int scrollSpeed=200;
	
//	private Animation myAnimation=null;
	
	public MyWebView(Context context) {
		super(context);
		this.mGestureDetector=new GestureDetector(this);
		
//		this.myAnimation=AnimationUtils.loadAnimation(context, R.anim.myanim);
	}
	
	public MyWebView(Context context,int screenWidth) {
		super(context);
		this.mGestureDetector=new GestureDetector(this);
		this.screenWidth=screenWidth;
		
//		this.myAnimation=AnimationUtils.loadAnimation(context, R.anim.myanim);
	}

	public boolean onDown(MotionEvent e) {
		Log.i("tag", "webview ondowm");
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		
		Log.i("tag", "webview onfling");
		Log.i("tag", "velocityX="+velocityX);
		Log.i("tag", "e1.getX()-e2.getX()="+(e1.getX()-e2.getX()));
		Log.i("tag", "screenWidth/3="+(screenWidth/3));
		MyViewPager parentPager=(MyViewPager) this.getParent();
		int curItem=parentPager.getCurrentItem();
		int preItem=curItem-1;
		int nexItem=curItem+1;
		if(e1.getX()-e2.getX()>this.screenWidth/4){
			if(this.canGoForward()){
				Toast.makeText(this.getContext(), "网页前进", Toast.LENGTH_SHORT).show();
				Log.i("tag", "webview goforward");
				this.goForward();
			}else{
				Toast.makeText(this.getContext(), "下一页", Toast.LENGTH_SHORT).show();
				parentPager.setCurrentItem(nexItem);
//				Log.i("tag", "开始动画");
//				this.startAnimation(myAnimation);
//				Log.i("tag", "结束动画");
			}
		}else if(e2.getX()-e1.getX()>this.screenWidth/4){
			if(this.canGoBack()){
				Toast.makeText(this.getContext(), "网页后退", Toast.LENGTH_SHORT).show();
				Log.i("tag", "webview goback");
				this.goBack();
			}else{
				Toast.makeText(this.getContext(), "上一页", Toast.LENGTH_SHORT).show();
				Log.i("tag", "webview nogoback");
				parentPager.setCurrentItem(preItem);
			}
		}else{		
			Log.i("tag", "no toleft or toright");
		}
		
		return false;
		
	}

	public void onLongPress(MotionEvent e) {
		Log.i("tag", "webview onlongpress");
		
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		Log.i("tag", "webview onscroll");	
		return false;
	}

	public void onShowPress(MotionEvent e) {
		Log.i("tag", "webview onshowpress");
		
	}

	public boolean onSingleTapUp(MotionEvent e) {
		Log.i("tag", "webview onsingletapup");
		return false;
	}

	public boolean onTouch(View v, MotionEvent event) {
		Log.i("tag", "webview ontouch");
		return this.mGestureDetector.onTouchEvent(event);
	}
	
	public boolean getMyTag(){
		return this.myTag;
	}

}
